<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 11:20
 */

namespace HiCommon\Repository;

use HiCommon\Model\OrderCheckpoint;
use HiCommon\Model\OrderCheckpointRefund;
use HiCommon\Model\OrderCheckpointVehicle;
use HiCommon\Model\OrderRepairstore;
use HiCommon\Model\StoreOrder;
use HiCommon\Model\StoreOrderRefund;
use Illuminate\Support\Facades\DB;

class ShopCountRepository extends BaseRepository
{

    /**
     * 获取[检测站]指定[日期区间]接单数
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function ck_order_take_num_by_date_block($arr_date = [], $arr_search = [])
    {
        //只要付款过的订单都算接单
        $arr_where     = ['pay_state' => 2];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('count(*) as num')];
        $result = OrderCheckpoint::where($arr_where)
                                 ->where(
                                   function ($query)
                                   use ($arr_search) {
                                       $n_shop_id = intval($arr_search['shop_id']);
                                       if ($n_shop_id > 0) {
                                           $query->where('shop_id', $n_shop_id);
                                       }
                                   }
                                 )
                                 ->where(
                                   function($query)
                                   use($arr_search) {
                                       $n_is_call_helpdriving = intval($arr_search['is_call_helpdriving']);
                                       if ($n_is_call_helpdriving > 0) {
                                           $query->whereHas('check_vehicle',
                                             function ($query)
                                             use($n_is_call_helpdriving){
                                                 $query->where('is_call_helpdriving',$n_is_call_helpdriving);
                                             });
                                       }
                                   }
                                 )
                                 ->whereBetween('created_at', $arr_between)
                                 ->groupBy('date')
                                 ->get($arr_select);
        return $result;
    }

    /**
     * 获取[检测站]指定[日期区间]交易总额
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function ck_pay_price_by_date_block($arr_date = [], $arr_search = [])
    {
        $arr_where     = ['pay_state' => 2];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('sum(price) as pay_price')];
        $res           = OrderCheckpoint::where($arr_where)
                                        ->where(
                                          function ($query)
                                          use ($arr_search) {
                                              $n_shop_id = intval($arr_search['shop_id']);
                                              if ($n_shop_id > 0) {
                                                  $query->where('shop_id', $n_shop_id);
                                              }
                                          }
                                        )
                                        ->whereBetween('created_at', $arr_between)
                                        ->groupBy('date')
                                        ->orderBy('date', 'desc')
                                        ->get($arr_select);
        return $res;
    }

    /**
     * 获取[检测站]指定[日期区间]退款总额
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function ck_refund_price_by_date_block($arr_date = [], $arr_search = [])
    {
        $arr_where     = ['apply_refund_state' => 3];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(refund_time,"' . $s_date_format . '") as date'), DB::raw('sum(refund_price) as refund_price')];

        $res = OrderCheckpointRefund::where($arr_where)
                                     ->where(
                                       function ($query)
                                       use ($arr_search) {
                                           $n_shop_id = intval($arr_search['shop_id']);

                                           if ($n_shop_id > 0) {
                                               $query->whereHas('order_checkpoint',
                                                 function ($query)
                                                 use ($n_shop_id) {
                                                     $query->where('shop_id', $n_shop_id);
                                                 });
                                           }
                                       }
                                     )
                                     ->whereBetween('refund_time', $arr_between)
                                     ->groupBy('date')
                                     ->orderBy('date', 'desc')
                                     ->get($arr_select);
        return $res;
    }

    /**
     * 获取[维修店]指定[日期区间]服务订单-接单数
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rs_service_order_take_num_by_date_block($arr_date = [], $arr_search = [])
    {
        //服务订单接单
        $arr_where     = [];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('count(*) as num')];
        $result        = OrderRepairstore::where($arr_where)
                                         ->where(
                                           function ($query)
                                           use ($arr_search) {
                                               $n_shop_id = intval($arr_search['shop_id']);
                                               if ($n_shop_id > 0) {
                                                   $query->where('shop_id', $n_shop_id);
                                               }
                                           }
                                         )
                                         ->whereIn('pay_status', [1, 2, 3, 4])
                                         ->whereBetween('created_at', $arr_between)
                                         ->groupBy('date')
                                         ->get($arr_select);
        return $result;
    }

    /**
     * 获取[维修店]指定[日期区间]商城订单-接单数
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rs_goods_order_take_num_by_date_block($arr_date = [], $arr_search = [])
    {
        //商城订单接单
        $arr_where     = ['pay_status' => 2];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('count(*) as num')];
        $result        = StoreOrder::where($arr_where)
                                   ->where(
                                     function ($query)
                                     use ($arr_search) {
                                         $n_shop_id = intval($arr_search['shop_id']);
                                         if ($n_shop_id > 0) {
                                             $query->where('shop_id', $n_shop_id);
                                         }
                                     }
                                   )
                                   ->whereBetween('created_at', $arr_between)
                                   ->groupBy('date')
                                   ->get($arr_select);
        return $result;
    }

    /**
     * 获取[维修店]指定[日期区间]服务订单-交易总额
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rs_service_order_pay_price_by_date_block($arr_date = [], $arr_search = [])
    {
        //服务订单交易金额
        $arr_where     = [];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('sum(pay_price) as sum_pay_price')];
        $result        = OrderRepairstore::where($arr_where)
                                         ->where(
                                           function ($query)
                                           use ($arr_search) {
                                               $n_shop_id = intval($arr_search['shop_id']);
                                               if ($n_shop_id > 0) {
                                                   $query->where('shop_id', $n_shop_id);
                                               }
                                           }
                                         )
                                         ->whereIn('pay_status', [1, 2, 3, 4])
                                         ->whereBetween('created_at', $arr_between)
                                         ->groupBy('date')
                                         ->get($arr_select);
        return $result;
    }

    /**
     * 获取[维修店]指定[日期区间]商城订单-交易总额
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rs_goods_order_pay_price_by_date_block($arr_date = [], $arr_search = [])
    {
        //商城订单交易金额
        $arr_where     = ['pay_status' => 2];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('sum(pay_amount) as sum_pay_price')];
        $result        = StoreOrder::where($arr_where)
                                   ->where(
                                     function ($query)
                                     use ($arr_search) {
                                         $n_shop_id = intval($arr_search['shop_id']);
                                         if ($n_shop_id > 0) {
                                             $query->where('shop_id', $n_shop_id);
                                         }
                                     }
                                   )
                                   ->whereBetween('created_at', $arr_between)
                                   ->groupBy('date')
                                   ->get($arr_select);
        return $result;
    }

    /**
     * 获取[维修店]指定[日期区间]服务订单-退款总额
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rs_service_order_refund_price_by_date_block($arr_date = [], $arr_search = [])
    {
        $arr_where     = ['pay_status' => 2];
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(refund_time,"' . $s_date_format . '") as date'), DB::raw('sum(refund_price) as sum_refund_price')];
        $result        = OrderRepairstore::where($arr_where)
                                         ->where(
                                           function ($query)
                                           use ($arr_search) {
                                               $n_shop_id = intval($arr_search['shop_id']);
                                               if ($n_shop_id > 0) {
                                                   $query->where('shop_id', $n_shop_id);
                                               }
                                           }
                                         )
                                         ->whereBetween('refund_time', $arr_between)
                                         ->groupBy('date')
                                         ->get($arr_select);
        return $result;
    }

    /**
     * 获取[维修店]指定[日期区间]商城订单-退款总额
     *
     * @param array $arr_date
     * @param array $arr_search
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function rs_goods_order_refund_price_by_date_block($arr_date = [], $arr_search = [])
    {
        $arr_where = ['apply_state' => 2];
        //        if($n_shop_id > 0){
        //            $arr_where['shop_id'] = $n_shop_id;
        //        }
        $arr_date      = format_date($arr_date);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(deal_at,"' . $s_date_format . '") as date'), DB::raw('sum(refund_amount) as sum_refund_price')];
        $result        = StoreOrderRefund::where($arr_where)
                                         ->where(
                                           function ($query)
                                           use ($arr_search) {
                                               $n_shop_id = intval($arr_search['shop_id']);

                                               if ($n_shop_id > 0) {
                                                   $query->whereHas('store_order',
                                                     function ($query)
                                                     use ($n_shop_id) {
                                                         $query->where('shop_id', $n_shop_id);
                                                     });
                                               }
                                           }
                                         )
                                         ->whereBetween('deal_at', $arr_between)
                                         ->groupBy('date')
                                         ->get($arr_select);

        return $result;
    }


}